package user.zc.helper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class ReflectUtil {
    public static <T> List<Field> fields(Class<T> cla){
        List<Field> fields = new ArrayList<>();
        for(Class clazz=cla;clazz!=Object.class;clazz=clazz.getSuperclass()) {
            for (Field declaredField : clazz.getDeclaredFields()) {
                fields.add(declaredField);
            }
        }
        return fields;
    }

    public static <T> List<Field> fields(Class<T> cla, Predicate<Field> predicate){
        List<Field> fields = new ArrayList<>();
        for(Class clazz=cla;clazz!=Object.class;clazz=clazz.getSuperclass()) {
            for (Field declaredField : clazz.getDeclaredFields()) {
                if(predicate.test(declaredField)){
                    fields.add(declaredField);
                }
            }
        }
        return fields;
    }
}
